package com.atsmart.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.atsmart.app.BizApp;
import com.atsmart.app.R;
import com.atsmart.app.model.MessageInfo;
import com.atsmart.app.model.MessageListInfo;
import com.atsmart.app.utils.StringUtil;

public class MessageAdapter extends LoadingAdapter {
	private static final int VIEWTYPE_UP = 1;
	private static final int VIEWTYPE_DOWN = 2;
	private static final int VIEWTYPE_LOADING = 3;

	private MessageListInfo datasource;
	private LayoutInflater inflater;

	public MessageAdapter(MessageListInfo datasource){
		this.datasource = datasource;
		inflater = (LayoutInflater) BizApp.getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void appendDataSource(MessageListInfo appendData){
		datasource.getMsgs().addAll(appendData.getMsgs());
	}

	private static class ViewHolder{
		TextView sender;
		TextView time;
		TextView content;
	}

	@Override
	public int getItemViewType(int position) {
		if (delegate != null && delegate.needLoading() && position == getCount() - 1) {
			return VIEWTYPE_LOADING;
		}
		MessageInfo msginInfo = datasource.getMsgs().get(position);
		if (msginInfo.getType() == 2||msginInfo.getType() == 3) {
			return VIEWTYPE_UP;
		}
		return VIEWTYPE_DOWN;
	}

	@Override
	public int getViewTypeCount() {
		return 4;
	}

	@Override
	public int getCount() {
		if (datasource.getMsgs() == null || datasource.getMsgs().size() == 0) {
            return 0;
        }

        if (delegate != null && delegate.needLoading()) {
            return datasource.getMsgs().size() + 1;
        } else {
            return datasource.getMsgs().size();
        }
	}

	@Override
	public Object getItem(int position) {
		 if (datasource.getMsgs() == null || position == datasource.getMsgs().size()
	                && delegate != null && delegate.needLoading()) {
	            return null;
	        }
		return datasource.getMsgs().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View loadingRow = super.getView(position, convertView, parent);
        if (loadingRow != null) {
            return loadingRow;
        }
        return getView(position, convertView);
	}

	private View getView(int position, View convertView){
		ViewHolder vh = null;
		if (getItemViewType(position) == VIEWTYPE_DOWN) {
			if (convertView == null || convertView.getTag() == null) {
				convertView = inflater.inflate(R.layout.message_list_item_down_layout, null);
				vh = new ViewHolder();
				vh.content = (TextView) convertView.findViewById(R.id.send_content);
				vh.sender = (TextView) convertView.findViewById(R.id.sender);
				vh.time = (TextView) convertView.findViewById(R.id.send_time);
				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}
		} else if (getItemViewType(position) == VIEWTYPE_UP){
			if (convertView == null || convertView.getTag() == null) {
				convertView = inflater.inflate(R.layout.message_list_item_up_layout, null);
				vh = new ViewHolder();
				vh.content = (TextView) convertView.findViewById(R.id.send_content);
				vh.sender = (TextView) convertView.findViewById(R.id.sender);
				vh.time = (TextView) convertView.findViewById(R.id.send_time);
				convertView.setTag(vh);
			} else {
				vh = (ViewHolder) convertView.getTag();
			}
		}

		
		
		MessageInfo msg = datasource.getMsgs().get(position);
		if(msg.getSticky()==1)
			convertView.setBackgroundColor(R.drawable.blue);
		if (msg.getType() == 2||msg.getType() == 3) {
			vh.sender.setText(StringUtil.formatDate(msg.getDate()));
			vh.content.setText(msg.getTitle());
			vh.time.setText(msg.getSender());
		} else {
			vh.sender.setText(msg.getSender());
			vh.content.setText(msg.getTitle());
			vh.time.setText(StringUtil.formatDate(msg.getDate()));
		}
		
		return convertView;
	}

	@Override
	protected boolean isLoadingRow(int position) {
		return position >= datasource.getMsgs().size();
	}



}
